#include "HeartbeatPanel.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QRadioButton>
#include <QTextEdit>
#include <QRandomGenerator>
#include "../../../../models/HeartbeatDetectionData.h"
#include "../../../../models/CommonData.h"
#include "../../../../controller/SignalBus.h"
#include "../../../../network/HttpClient.h"


HeartbeatPanel::HeartbeatPanel()
{
    initComponents();
    client = new HttpClient();
    connect(client,&HttpClient::responseReceived,this,&HeartbeatPanel::onReciveResponse);

}

void HeartbeatPanel::onSendRequest()
{
    int randomNumber = QRandomGenerator::global()->bounded(10000,100000);
    QString dateTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmssSSS");
    QString transIdStr = QString("%1%2").arg(dateTime).arg(randomNumber);
    QString dateTimeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
    std::string status = requestVariableHash["connectStatus"]->text().toStdString();

    BaseData<HeartbeatDetectionData>::execute(&HeartbeatDetectionData::setConnectStatus,status);
    BaseData<CommonData>::execute(&CommonData::setEventTime,dateTimeStr.toStdString());
    BaseData<CommonData>::execute(&CommonData::setTransId,transIdStr.toStdString());

    QJsonObject json;
    json["factoryNo"] = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getFactoryNo));
    json["transId"] = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getTransId));
    json["eventTime"] = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getEventTime));
    json["eventUser"] = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getEventUser));
    json["machineNo"] = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getMachineNo));
    json["connectStatus"] = requestVariableHash["connectStatus"]->text();

    std::string fullUrl = BaseData<CommonData>::execute(&CommonData::setApiUrl,"/heartbeat");
    client->PostAysnc(QString::fromStdString(fullUrl),json);

    jsonHash["requestBody"]->setPlainText(QJsonDocument(json).toJson());

    qDebug()<<"[CheckIn] request:"<<json;
    qDebug()<<"[CheckIn] url:" <<fullUrl;

}

void HeartbeatPanel::onReciveResponse(const QByteArray &json)
{
    qDebug()<<"[CheckIn] response:"<<json;
    QJsonDocument doc = QJsonDocument::fromJson(json);
    if(!doc.isNull())
    {
        QJsonObject obj = doc.object();
        int resultCode = obj["resultCode"].toInt();

        responseVariableHash["code"]->setText(QString::number(resultCode));
        responseVariableHash["message"]->setText(obj["resultMsg"].toString());
        jsonHash["responseBody"]->setPlainText(doc.toJson());
    }
}

void HeartbeatPanel::initComponents()
{
    const auto layout = new QGridLayout();
    layout->addWidget(initRequestPanel(),0,0,1,1);
    layout->addWidget(initResponsePanel(),1,0,1,1);
    setLayout(layout);
    setTitle(tr("心跳检测"));

}

QWidget *HeartbeatPanel::initRequestPanel()
{
    const auto lbStatus = new QLabel(tr("连接状态"));
    const auto rbtnConnect = new QRadioButton(tr("连接"));
    const auto rbtnDisconnect = new QRadioButton(tr("离线"));
    const auto lineStatus = new QLineEdit();
    const auto lbJson = new QLabel(tr("请求体json"));
    const auto textJson = new QTextEdit();
    const auto statusLayout = new QHBoxLayout();
    const auto btnExecute = new QPushButton(tr("发送请求"));

    statusLayout->addWidget(lbStatus);
    statusLayout->addWidget(rbtnConnect);
    statusLayout->addWidget(rbtnDisconnect);
    statusLayout->addWidget(lineStatus);
    lineStatus->setReadOnly(true);

    const auto layout = new QGridLayout();
    layout->addLayout(statusLayout,0,0,1,1);
    layout->addWidget(lbJson,1,0,1,2);
    layout->addWidget(textJson,2,0,1,2);
    layout->addWidget(btnExecute,0,1,1,1);

    requestVariableHash.insert("connectStatus",lineStatus);
    jsonHash.insert("requestBody",textJson);

    connect(rbtnConnect,&QRadioButton::clicked,this,[=](){lineStatus->setText("1");});
    connect(rbtnDisconnect,&QRadioButton::clicked,this,[=](){lineStatus->setText("2");});
    connect(btnExecute,&QPushButton::clicked,this,&HeartbeatPanel::onSendRequest);
    const auto widget = new QWidget();
    widget->setLayout(layout);
    return widget;
}

QWidget *HeartbeatPanel::initResponsePanel()
{
    const auto lbCode = new QLabel(tr("结果代码"));
    const auto lineCode = new QLineEdit();
    const auto lbMessage = new QLabel(tr("结果消息"));
    const auto lineMessage = new QLineEdit();
    const auto lbJson = new QLabel(tr("响应体Json"));
    const auto textJson = new QTextEdit();

    const auto layout = new QGridLayout();
    layout->addWidget(lbCode,0,0,1,1);
    layout->addWidget(lineCode,0,1,1,1);
    layout->addWidget(lbMessage,1,0,1,1);
    layout->addWidget(lineMessage,1,1,1,1);
    layout->addWidget(lbJson,2,0,1,1);
    layout->addWidget(textJson,3,0,1,2);

    responseVariableHash.insert("code",lineCode);
    responseVariableHash.insert("message",lineMessage);
    jsonHash.insert("responseBody",textJson);

    const auto widget = new QWidget();
    widget->setLayout(layout);
    return widget;
}
